/*FOR NEW UI*/
var jsonUserData = {
    search_value: "",
}
var jsonDeactivateUserData = {
    search_value: "",   
}
var columnSortActivate = [];
var columnSortDeactivate = [];
$(document).ready(function(){
        
        
        
    // Load users lis on Table
    var pathname = window.location.pathname;
    if (pathname=="/settings") {
        load.loadTableUser();
        load.loadTableDeactivatedUser();
        
    }
    
    if (pathname=="/user_view/user_settings") {
        /*  Active  */
        // onload
        GetUserDataTable.defaultData();
        // search
        $("body").on("keyup","#txtActivatedUserDatatable",function(e){
            $("#loadUser").dataTable().fnDestroy();
            jsonUserData['search_value'] = $(this).val();
            GetUserDataTable.defaultData();
            $("#loadUser").css("width","100%");
        })
        // sort
        $("body").on("click","#loadUser th",function(){
            var cursor = $(this).css("cursor");
            jsonUserData['column-sort'] = $(this).attr("field_name");
            if($(this).attr("field_name")=="" || $(this).attr("field_name")==undefined){
                return;
            }
            var indexcolumnSort = columnSortActivate.indexOf($(this).attr("field_name"));

            $(this).closest("#loadUser").find(".sortable-image").html("");
            if(indexcolumnSort==-1){
                columnSortActivate.push($(this).attr("field_name"))
                $(this).find(".sortable-image").html("<i class='fa fa-sort-asc'></i>")
                jsonUserData['column-sort-type'] = "ASC";
            }else{
                columnSortActivate.splice(indexcolumnSort,1);
                $(this).find(".sortable-image").html("<i class='fa fa-sort-desc'></i>")
                jsonUserData['column-sort-type'] = "DESC";
            }
            
            $("#loadUser").dataTable().fnDestroy();
            GetUserDataTable.defaultData();
        })


        /*  Deactivate  */
        // onload
        GetDeactivateUserDataTable.defaultData();
        // search
        $("body").on("keyup","#txtDeactivatedUserDatatable",function(e){
            $("#loadDeactivatedUser").dataTable().fnDestroy();
            jsonDeactivateUserData['search_value'] = $(this).val();
            GetDeactivateUserDataTable.defaultData();
            $("#loadDeactivatedUser").css("width","100%");
        })
        // sort
        $("body").on("click","#loadDeactivatedUser th",function(){
            var cursor = $(this).css("cursor");
            jsonDeactivateUserData['column-sort'] = $(this).attr("field_name");
            if($(this).attr("field_name")=="" || $(this).attr("field_name")==undefined){
                return;
            }
            var indexcolumnSort = columnSortDeactivate.indexOf($(this).attr("field_name"));

            $(this).closest("#loadDeactivatedUser").find(".sortable-image").html("");
            if(indexcolumnSort==-1){
                columnSortDeactivate.push($(this).attr("field_name"))
                $(this).find(".sortable-image").html("<i class='fa fa-sort-asc'></i>")
                jsonDeactivateUserData['column-sort-type'] = "ASC";
            }else{
                columnSortDeactivate.splice(indexcolumnSort,1);
                $(this).find(".sortable-image").html("<i class='fa fa-sort-desc'></i>")
                jsonDeactivateUserData['column-sort-type'] = "DESC";
            }
            
            $("#loadDeactivatedUser").dataTable().fnDestroy();
            GetDeactivateUserDataTable.defaultData();
        })
    }

    if (pathname=="/audit_logs") {
        load.loadAuditLogs();
    }
        

        
    // Load User onclick
    $("body").on("click",".showAllUser",function(){
        $("#loadUser").dataTable().fnDestroy(); 
        load.loadTableUser();
      
    });
    
    // Load Deactivated User onclick
    $("body").on("click",".showAllUserDeactivatedUser",function(){
        $("#loadDeactivatedUser").dataTable().fnDestroy(); 
        load.loadTableDeactivatedUser();
        $("#loadDeactivatedUser").css("width","100%");
    });
    
});

var mTb;
load = {
        loadTableUser : function(){
                var oTable = $('#loadUser').dataTable( {
                     "bProcessing": true,
                     "bRetrieve": true,
                     //"bPaginate": true,//"sEcho":3,//"iTotalDisplayRecords":10,//"iTotalRecords":"10",
                     //"iTotalDisplayRecords":"10",//"bServerSide": true,
                     "sAjaxSource": "/ajax/load",
                     "sPaginationType": "full_numbers",
                     "sAjaxDataProp": "company_user",
                     "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
                     "aoColumns": [
                         //{ "mData": "" },
                        // { "mData": "image" },
                         // { "mData": "email" },
                         // { "mData": "displayName" },
                         // { "mData": "fullname" },
                         // { "mData": "position" },
                         // { "mData": "department" },
                         // { "mData": "dateRegistered" },
                           { 
                            "mData": function(source, type, val) {
                                return '<div align="center">'+ source['image'] +'</div>';
                            }
                         },
                         { 
                            "mData": function(source, type, val) {
                                return '<div class="fl-table-ellip">'+ source['email'] +'</div>';
                            }
                         },
                         { 
                            "mData": function(source, type, val) {
                                return '<div class="fl-table-ellip">'+ source['displayName'] +'</div>';
                            }
                         },
                         { 
                            "mData": function(source, type, val) {
                                return '<div class="fl-table-ellip">'+ source['fullname'] +'</div>';
                            }
                         },
                         { 
                            "mData": function(source, type, val) {
                                return '<div class="fl-table-ellip">'+ source['position'] +'</div>';
                            }
                         },
                         { 
                            "mData": function(source, type, val) {
                                return '<div class="fl-table-ellip">'+ source['department'] +'</div>';
                            }
                         },
                         { 
                            "mData": function(source, type, val) {
                                return '<div class="fl-table-ellip">'+ source['dateRegistered'] +'</div>';
                            }
                         },
                         { "mData": "actions" }
                     ],
                     "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
                         $("#example_length > label > select").addClass("selectlength");
                     },
                     "aoColumnDefs": [ {
                         "bSortable": true, "aTargets": [ 1,2,3,4,5,6,7 ]
                         },{
                             "bSortable": false, "aTargets": [ "_all" ]
                         }/*, {
                             "bSearchable": true, "aTargets": [ 0 ]
                         }, {
                             "bSearchable": false, "aTargets": [ "_all" ]
                     }*/
                     ],
                     "fnServerData": function (sSource, aoData, fnCallback) {
                         $.ajax({
                             type: "POST",
                             cache: false,
                             dataType: 'json',
                             url: sSource,
                             data: {action:"companyUserActive"},
                             success: function (json) {
                                 // Retrieve data
                                 //console.log(json)
                                 fnCallback(json);
                                 brokenImage()
                             }
                         });
                     }
                 });
                return oTable;
        },
        
        // Load Deactivated User
        loadTableDeactivatedUser : function(){
                var oTable = $('#loadDeactivatedUser').dataTable( {
                     "bProcessing": true,
                     "bRetrieve": true,
                     //"bPaginate": true,//"sEcho":3,//"iTotalDisplayRecords":10,//"iTotalRecords":"10",
                     //"iTotalDisplayRecords":"10",//"bServerSide": true,
                     "sAjaxSource": "/ajax/load",
                     "sPaginationType": "full_numbers",
                     "sAjaxDataProp": "company_user",
                     "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
                     "aoColumns": [
                         //{ "mData": "" },
                         //{ "mData": "image" },
                         /*{ "mData": "email" },
                         { "mData": "displayName" },
                         { "mData": "fullname" },
                         { "mData": "position" },
                         { "mData": "department" },
                         { "mData": "dateRegistered" },
                         { "mData": "actions" }*/
                          { 
                            "mData": function(source, type, val) {
                                return '<div align="center">'+ source['image'] +'</div>';
                            }
                         },
                         { 
                            "mData": function(source, type, val) {
                                return '<div class="fl-table-ellip">'+ source['email'] +'</div>';
                            }
                         },
                         { 
                            "mData": function(source, type, val) {
                                return '<div class="fl-table-ellip">'+ source['displayName'] +'</div>';
                            }
                         },
                         { 
                            "mData": function(source, type, val) {
                                return '<div class="fl-table-ellip">'+ source['fullname'] +'</div>';
                            }
                         },
                         { 
                            "mData": function(source, type, val) {
                                return '<div class="fl-table-ellip">'+ source['position'] +'</div>';
                            }
                         },
                         { 
                            "mData": function(source, type, val) {
                                return '<div class="fl-table-ellip">'+ source['department'] +'</div>';
                            }
                         },
                         { 
                            "mData": function(source, type, val) {
                                return '<div class="fl-table-ellip">'+ source['dateRegistered'] +'</div>';
                            }
                         },
                         { "mData": "actions" }
                     ],
                     "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
                         $("#example_length > label > select").addClass("selectlength");
                     },
                     "aoColumnDefs": [ {
                         "bSortable": true, "aTargets": [ 1,2,3,4,5,6,7 ]
                         },{
                             "bSortable": false, "aTargets": [ "_all" ]
                         }/*, {
                             "bSearchable": true, "aTargets": [ 0 ]
                         }, {
                             "bSearchable": false, "aTargets": [ "_all" ]
                     }*/
                     ],
                     "fnServerData": function (sSource, aoData, fnCallback) {
                         $.ajax({
                             type: "POST",
                             cache: false,
                             dataType: 'json',
                             url: sSource,
                             data: {action:"companyUserDeactivated"},
                             success: function (json) {
                                 // Retrieve data
                                 //console.log(json)
                                 fnCallback(json);
                                 brokenImage()
                             }
                         });
                     }
                 });
                return oTable;
        },
        // Load Deactivated User
        loadAuditLogs : function(){
                var oTable = $('#loadAuditLogs').dataTable( {
                     "bProcessing": true,
                     "bRetrieve": true,
                     //"bPaginate": true,//"sEcho":3,//"iTotalDisplayRecords":10,//"iTotalRecords":"10",
                     //"iTotalDisplayRecords":"10",//"bServerSide": true,
                     "sAjaxSource": "/ajax/load",
                     "sPaginationType": "full_numbers",
                     "sAjaxDataProp": "company_user",
                     "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
                     "aoColumns": [
                         { "mData": "userCheckbox" },
                         { "mData": "image" },
                         { "mData": "fullname" },
                         { "mData": "action" },
                         //{ "mData": "tablename" },
                         { "mData": "type" },
                         { "mData": "dateRegistered" },
                         { "mData": "ip" },
                         { "mData": "actions" }
                     ],
                     "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
                         $("#example_length > label > select").addClass("selectlength");
                     },
                     "aoColumnDefs": [ {
                         "bSortable": true, "aTargets": [ 1,2,3,4,5,6,7 ]
                         },{
                             "bSortable": false, "aTargets": [ "_all" ]
                         }/*, {
                             "bSearchable": true, "aTargets": [ 0 ]
                         }, {
                             "bSearchable": false, "aTargets": [ "_all" ]
                     }*/
                     ],
                     "fnServerData": function (sSource, aoData, fnCallback) {
                        
                         $.ajax({
                             type: "POST",
                             cache: false,
                             dataType: 'json',
                             url: sSource,
                             data: {action:"auditLogs"},
                             success: function (json) {
                                //console.log(json)
                                 // Retrieve data
                                 //console.log(json)
                                 fnCallback(json);
                                 brokenImage()
                             }
                         });
                     }
                 });
                return oTable;
        }
}
/*New ui*/
GetUserDataTable = {
    "defaultData" : function(){
        var oTable = $("#loadUser").dataTable({
            "sDom": 'Rlfrtip',
            // "sScrollY": "400px",
            "oLanguage": {
                "sProcessing": '<div class="spinner load_m"> '+
                       '<div class="bar1"></div> '+
                       '<div class="bar2"></div> '+
                       '<div class="bar3"></div> '+
                       '<div class="bar4"></div> '+
                       '<div class="bar5"></div> '+
                       '<div class="bar6"></div> '+
                      '<div class="bar7"></div> '+
                      '<div class="bar8"></div> '+
                      '<div class="bar9"></div> '+
                      '<div class="bar10"></div> '+
                 '</div>'
            },
            "oColReorder" : false,
            "bLengthChange": false,
            "bFilter": false,
            "bProcessing": true,
            "bServerSide": true,
            "sPaginationType": "full_numbers",
            "bSort": false,
            "sAjaxSource": "/ajax/load",
            "fnServerData": function(sSource, aoData, fnCallback) {
                aoData.push({"name": "action", "value": "companyUserActiveDatatable"});
                aoData.push({"name": "start", "value": "0"});
                aoData.push({"name": "search_value", "value": jsonUserData['search_value']});
                aoData.push({"name": "column-sort", "value": jsonUserData['column-sort']});
                aoData.push({"name": "column-sort-type", "value": jsonUserData['column-sort-type']});
                // aoData.push({"name": "column-sort", "value": ""});
                // aoData.push({"name": "column-sort-type", "value": ""});
                $.ajax({
                    "dataType": 'json',
                    "type": "POST",
                    "url": sSource,
                    "data": aoData,
                    "success": function(data) {
                        // console.log(data)
                        // return;
                        fnCallback(data);
                    }
                });
            },
            "fnInitComplete": function(oSettings, json) {
                
            }
        });
        return oTable;
    }
}
GetDeactivateUserDataTable = {
    "defaultData" : function(){
        var oTable = $("#loadDeactivatedUser").dataTable({
            "sDom": 'Rlfrtip',
            // "sScrollY": "400px",
            "oLanguage": {
                "sProcessing": '<div class="spinner load_m"> '+
                       '<div class="bar1"></div> '+
                       '<div class="bar2"></div> '+
                       '<div class="bar3"></div> '+
                       '<div class="bar4"></div> '+
                       '<div class="bar5"></div> '+
                       '<div class="bar6"></div> '+
                      '<div class="bar7"></div> '+
                      '<div class="bar8"></div> '+
                      '<div class="bar9"></div> '+
                      '<div class="bar10"></div> '+
                 '</div>'
            },
            "oColReorder" : false,
            "bLengthChange": false,
            "bFilter": false,
            "bProcessing": true,
            "bServerSide": true,
            "sPaginationType": "full_numbers",
            "bSort": false,
            "sAjaxSource": "/ajax/load",
            "fnServerData": function(sSource, aoData, fnCallback) {
                aoData.push({"name": "action", "value": "companyUserDeactivatedDatatable"});
                aoData.push({"name": "start", "value": "0"});
                aoData.push({"name": "search_value", "value": jsonDeactivateUserData['search_value']});
                aoData.push({"name": "column-sort", "value": jsonDeactivateUserData['column-sort']});
                aoData.push({"name": "column-sort-type", "value": jsonDeactivateUserData['column-sort-type']});
                // aoData.push({"name": "column-sort", "value": ""});
                // aoData.push({"name": "column-sort-type", "value": ""});
                $.ajax({
                    "dataType": 'json',
                    "type": "POST",
                    "url": sSource,
                    "data": aoData,
                    "success": function(data) {
                        // console.log(data)
                        // return;
                        fnCallback(data);
                    }
                });
            },
            "fnInitComplete": function(oSettings, json) {
                
            }
        });
        return oTable;
    }
}